﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BaseLibrary;

namespace CodeToAddresConverter
{
    class Program
    {
        //На входе файл с программой для мэна, содержащей имена функций как коды
        //На выходе пишет в консоль такую же программу, но с адресами
        static void Main(string[] args)
        {
            if (args.Length > 0)
                using(var r = new StreamReader(args[0]))
                    Convert(r);
            //Convert(new StreamReader("input.txt"));
        }

        private static void Convert(StreamReader reader)
        {
            try
            {
                int n = 0;
                var commands = new List<Command>();
                var labels = new Dictionary<string, Label>();
                string curfun = "";
                string s;
                while ((s = reader.ReadLine()) != null)
                {
                    if (s.Trim().Length == 0)
                        continue;
                    var com = new Command(s);
                    if (!com.Function.IsEmpty())
                    {
                        if (!labels.ContainsKey(com.Function))
                            labels.Add(com.Function, new Label(com.Function));
                        labels[com.Function].Number = n;
                        curfun = com.Function;
                    }
                    else
                    {
                        if (!curfun.IsEmpty())
                        {
                            com.Function = curfun;
                            curfun = "";
                        }
                        if (com.Code == "LDF" || com.Code == "SEL" || com.Code == "TSEL")
                        {
                            if (!labels.ContainsKey(com.Param1))
                                labels.Add(com.Param1, new Label(com.Param1));
                            com.Label1 = labels[com.Param1];
                        }
                        if (com.Code == "SEL" || com.Code == "TSEL")
                        {
                            if (!labels.ContainsKey(com.Param2))
                                labels.Add(com.Param2, new Label(com.Param2));
                            com.Label2 = labels[com.Param2];
                        }
                        if (!com.Code.IsEmpty())
                        {
                            com.Number = n;
                            n++;
                        }
                        if (commands.Count != 0) commands[commands.Count - 1].NextCommand = com;
                        commands.Add(com);
                    }
                }
                int i = commands.Count - 1;
                while (i >= 0 && commands[i].Code.IsEmpty())
                    i--;
                commands.RemoveRange(i + 1, commands.Count - i - 1);

                foreach (var com in commands)
                    Console.WriteLine(com.Write());
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error: {0}", ex.Message);
            }
        }
    }
}
